package com.adv.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adv.controller.base.BaseController;
import com.adv.service.AdvertiserService;
import com.adv.vo.AdverposForm;

@Controller
public class AdvertiserController extends BaseController {

	@Autowired
	private AdvertiserService service;

	@RequestMapping("/advertiser/list")
	public String list(ModelMap model) {
		return "advertiser/list";
	}

	/**
	 * 获取完整json格式的数据
	 * 
	 * @return json
	 */
	@ResponseBody
	@RequestMapping("/advertiser/json")
	public Object json() {
		return service.getList();
	}

	@RequestMapping("/advertiser/add")
	public String add(AdverposForm form, ModelMap model) {
		if (null != form && null != form.getId()) {
			model.put("advertiser", service.getAdverposFormById(form.getId()));
		}
		return "advertiser/modify";
	}

	@ResponseBody
	@RequestMapping("/advertiser/save")
	public Object save(@Validated AdverposForm form, @Valid BindingResult result, ModelMap model) {
		if (result.hasErrors()) {
			// 新建需要校验完整字段
			return convertField2Map(result);
		}
		return service.saveAdvertiser(form);
	}

	@ResponseBody
	@RequestMapping("/advertiser/enable")
	public Object enable(AdverposForm form) {
		if (null != form.getId()) {
			form.setEnable(true);
			return service.editAdvertiser(form);
		}
		return failureMsg();
	}

	@ResponseBody
	@RequestMapping("/advertiser/disable")
	public Object disable(AdverposForm form) {
		if (null != form.getId()) {
			form.setEnable(false);
			return service.editAdvertiser(form);
		}
		return failureMsg();
	}

	@ResponseBody
	@RequestMapping("/advertiser/delete")
	public Object delete(AdverposForm form) {
		if (null != form.getId()) {
			return service.deleteAdvertiser(form);
		}
		return failureMsg();
	}

}
